{include file="public/header" /}
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <form id="newsFormData" class="layui-form layui-form-pane" autocomplete="off">
            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">基本内容</li>
                    <li>SEO内容</li>
                </ul>

                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form-item">
                            <label class="layui-form-label layui-required">标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" id="title" class="layui-input" placeholder="请输入标题">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-upload">
                                <label class="layui-form-label layui-required">封面图</label>
                                <div class="layui-upload">
                                    <div class="layui-input-block">
                                        <div class="upload-frame" id="uploadCover">
                                            <i class="layui-icon layui-icon-addition"></i>
                                        </div>
                                        <div class="preview-box">
                                            <div class="uploader-list" id="coverPreview"></div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                            <input type="hidden" name="cover" id="newsCover">
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="time" id="time" class="layui-input" placeholder="请选择日期">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">简述</label>
                            <div class="layui-input-block">
                                <textarea name="intro" placeholder="请输入简述" class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">内容</label>
                            <div class="layui-input-block">
                                <!--富文本-->
                                <script id="newsContent" name="content" type="text/plain"></script>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">排序</label>
                            <div class="layui-input-block">
                                <input type="text" name="sort" id="sort" value="255" class="layui-input" placeholder="请输入排序，数字越小，排序越靠前">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="status" id="status" checked lay-text="显示|隐藏" lay-skin="switch">
                                <input type="checkbox" name="is_recommended" id="isRecommended" lay-text="推荐|推荐" lay-skin="switch">
                            </div>
                        </div>
                    </div>
                    <div class="layui-tab-item">
                        <div class="layui-form-item">
                            <label class="layui-form-label">SEO标题</label>
                            <div class="layui-input-block">
                                <input type="text" name="seo_title" id="newsSeoTitle"  class="layui-input" placeholder="请输入SEO标题，长度尽量控制在60个字符内">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">SEO关键词</label>
                            <div class="layui-input-block">
                                <input type="text" name="seo_keywords" id="newsSeoKeywords" class="layui-input" placeholder="每个关键词用英文逗号隔开">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">SEO描述</label>
                            <div class="layui-input-block">
                                <textarea name="seo_describe" id="newsSeoDescribe" placeholder="请输入" class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">URL名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="seo_link" id="NewsSeoLink" class="layui-input" placeholder="自定义详情页URL名称，只允许输入英文字母、数字、- 和 _">
                            </div>
                        </div>
                    </div>

                    <div class="btn-block">
                        <button type="button" onclick="newsSendFormData()" class="layui-btn layui-btn-normal">提交</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
</body>

<!--UEdit-->
<!--这两个文件要注意引用顺序-->
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__STATIC__/ueditor/ueditor.config.js"></script>
<!-- 配置文件 -->
<script type="text/javascript" src="__STATIC__/ueditor/ueditor.all.js"></script>

<!--UEdit-->

<script type="text/javascript">
    var newsContent = UE.getEditor('newsContent');
</script>

<script>
    layui.use(['form', 'element', 'upload', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            upload = layui.upload,
            laydate = layui.laydate;

        //封面图
        uploadTemplate('#uploadCover','#coverPreview','#newsCover','cover','newCoverLoad')

        //日期选择器实例
        laydate.render({
            elem: "#time",
            theme: '#1E9FFF'
        })

        function uploadTemplate(btn,preview,value,flag,load){
            upload.render({
                elem: btn,
                url: "{:url('news/upload')}",
                multiple: false,
                accept: 'file',
                size: uploadFileSize,
                acceptMime: uploadImgExt,
                before: function (obj) {
                    $(btn).addClass('upload-btn-hide')
                    $(preview).empty()
                    load = layer.load(1, {shade: [0.5, '#FFF'], time: 0})//弹出遮罩层
                },
                done: function (res) {
                    layer.close(load)//关闭遮罩层
                    sucMsg(res.msg)
                    if (res.code == 'suc') {
                        $(preview).append(
                            '<div class="file-items">' +
                            '<div class="handle" data-flag="' + flag + '" data-path="' + res.url + '"><i class="layui-icon layui-icon-delete"></i></div>' +
                            '<img class="cover-preview" src="' + res.url + '">' +
                            '</div>'
                        );

                        $(value).val(res.url)
                    } else {
                        $(btn).removeClass('upload-btn-hide')
                    }
                }
            })
        }
    })

    //删除图片
    $(document).on("click", ".file-items .handle", function (event) {
        let flag = $(this).attr('data-flag')
        $('#uploadCover').removeClass('upload-btn-hide')//显示上传按钮
        $("#newsCover").val('')
        $(this).parent().remove();
    })

    function newsSendFormData() {
        let title = $("#title").val()
        let cover = $("#newsCover").val()

        if (emptyCheck(title)) {
            hintMsg('请输入标题', 'title')
            return false;
        } else if (emptyCheck(cover)) {
            hintMsg('请上传封面图')
            return false;
        }

        //弹出遮罩层，防止多次点击
        var newsAddLoad = layer.load(1, {shade: [0.5, '#FFF'], time: 0})
        let data = $("#newsFormData").serialize()
        ajaxRequest("{:url('news/add')}", data, function (res) {
            if (res.code == 'suc') {
                sucMsg(res.msg, function () {
                    //刷新父级页面
                    parent.layui.table.reload('newsList')
                    //关闭遮罩层
                    layer.close(newsAddLoad)
                    //关闭当前弹出层
                    xadmin.close()
                })
            } else {
                errMsg(res.msg, function () {
                    //关闭遮罩层
                    layer.close(newsAddLoad)
                })
            }
        })
    }
</script>